package org.fly.service.impl;

import org.fly.bean.CustomerLevel;
import org.fly.bean.query.CustomerLevelQuery;
import org.fly.dao.CustomerLevelDao;
import org.fly.service.CustomerLevelService;
import org.fly.utils.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.fly.bean.common.BusinessException;
import java.util.List;

/**
* Created by autoCode on 2018-01-31 20:25:40.
*/
@Service
public class CustomerLevelServiceImpl implements CustomerLevelService {

@Autowired
CustomerLevelDao customerLevelDao;

@Override
public List<CustomerLevel> list(CustomerLevelQuery query) {
        return customerLevelDao.list(query);
    }

    @Override
    public long count(CustomerLevelQuery query) {
        return customerLevelDao.count(query);
    }

    @Override
    public CustomerLevel findById(String id) {
        return customerLevelDao.findById(id);
    }

    @Override
    public void save(CustomerLevel bean)  throws BusinessException {

    if(bean.getDefaultSet()==1){
        customerLevelDao.updateDefaultFalse(bean.getSupplierId());
    }
    if(StringUtils.isNotBlank(bean.getId())){
         customerLevelDao.update(bean);
    }else {
        bean.setId(UUIDFactory.random());
        bean.setCreateTime(System.currentTimeMillis());
        customerLevelDao.insert(bean);
    }
    }

    @Override
    public void delete(String id) {
        customerLevelDao.delete(id);
    }
}
